package input;

import instance.Instance;
import instance.InstanceEj1;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import java.util.LinkedList;
import java.util.StringTokenizer;

import problem.ej1.Ruta;



public class Problem1Input implements Input{
	
	
	private String inputFile;
	private LinkedList<Instance> instances;
	
	public Problem1Input(String inputFile) {
		this.inputFile = inputFile;

	}
    //nos guardamos todas las instancias del archivo de entrada para procesarlos de a uno
	
	@Override
	public void readFile() throws Exception {
				
		BufferedReader bufferedReader = new BufferedReader(new FileReader(
				new File(inputFile)));

		instances = new LinkedList<Instance>();

		while (bufferedReader.ready()) {
			Instance instance =  readInstance(bufferedReader);
			instances.add( instance);
		}
		
		bufferedReader.close();	
	}

	@Override
	public LinkedList<Instance> getInstances() {
	
		return instances;
	}

	@Override
	public Instance readInstance(BufferedReader bufferedReader)
			throws Exception {

		LinkedList <Ruta> rutas = new LinkedList<Ruta>();
		InstanceEj1 subInstance ;
		String line = bufferedReader.readLine();
		StringTokenizer origen_y_destino = new StringTokenizer(line);
		String origen = origen_y_destino.nextToken().trim();
		String destino = origen_y_destino.nextToken().trim();
		
		line = bufferedReader.readLine();
		StringTokenizer tokens = new StringTokenizer(line, ";");
		
		while(tokens.hasMoreTokens()){
			String tup = tokens.nextToken().trim();
			StringTokenizer tupla =  new StringTokenizer(tup);

			String c1 =tupla.nextToken().trim();
			String c2 = tupla.nextToken().trim();
			int peso = Integer.parseInt(tupla.nextToken().trim()); 

			Ruta ruta = new Ruta(c1,c2,peso);
			rutas.add(ruta); 
		}
		subInstance = new InstanceEj1(rutas, origen, destino);

				
		return subInstance;
		
		
		
		
		
	}
		

}
